using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class LUUYDAL
    {
        private SqlConnection cn = null;
        public LUUYDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(LUUY ly)
        {
            SqlCommand cm = new SqlCommand("Luu_y_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@phieudientu", ly.Phieudientu);
            cm.Parameters.AddWithValue("@cachinhthuctt", ly.Cachinhthuctt);
            cm.Parameters.AddWithValue("@luuy", ly.Luuy);
            cm.ExecuteNonQuery();
        }
        public void Update(LUUY ly)
        {
            SqlCommand cm = new SqlCommand("Luu_y_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maluuy", ly.Maluuy);
            cm.Parameters.AddWithValue("@phieudientu", ly.Phieudientu);
            cm.Parameters.AddWithValue("@cachinhthuctt", ly.Cachinhthuctt);
            cm.Parameters.AddWithValue("@luuy", ly.Luuy);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Luu_y_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maluuy", iD);
            cm.ExecuteNonQuery();
        }
        public Luu_yCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Luu_y_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Luu_yCollection luuyCollection = new Luu_yCollection();


            while (dr.Read())
            {
                LUUY luuy = new LUUY();
                luuy.Maluuy = int.Parse(dr.GetValue(0).ToString());
                luuy.Phieudientu = dr.GetValue(1).ToString();
                 luuy.Cachinhthuctt = dr.GetValue(2).ToString();
                 luuy.Luuy = dr.GetValue(3).ToString();
                luuyCollection.Add(luuy);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return luuyCollection;
        }
    }
}
